/**
 * Created by evil on 17-2-23.
 */

$(function ()
{
    key.init();
    key.submit();
});

var key = {
    init: function ()
    {
        $('#setting-css').attr('href', '/static/css/setting_key.css');
    },
    submit: function ()
    {
        $('#submit').click(function ()
        {
            cleanInputHelp();
            var key = $('#key');

            if (isInputNull(key) == 0)
                return null;

            $(this).addClass('disabled');

            $.post('/setting/key/post', {
                key: key.val()
            }, function (data)
            {
                if (data == true)
                    toastr.success('提交成功！');
                else
                    toastr.error('提交失败！' + data);
                $('#submit').removeClass('disabled');
            });
        });
    }
};


function isInputNull(it, help)
{
    if (it.val() == null || it.val().toString().trim() == '')
    {
        it.parent().parent().addClass('has-error');
        if (help == null)
            it.parent().find('span').text('输入为空！');
        else
            it.parent().find('span').text(help);

        return 0;
    }
    return 1;
}

function cleanInputHelp()
{
    var input = $('.input');
    input.find('.form-group').removeClass('has-error');
    input.find('.help-block').html();
}
